package com.ys.controller.api.outer;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.amazon.SellingPartnerAPIAA.AWSAuthenticationCredentials;
import com.amazon.SellingPartnerAPIAA.AWSAuthenticationCredentialsProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bo.util.DateUtil;
import com.ys.entity.business.TpAmazonSeller;
import com.ys.enume.RegionEnum;
import com.ys.enume.business.TpAmazonSellerEnum;
import com.ys.service.amazonorder.TcoDayAllService;
import com.ys.service.amazonorder.TcoDayCountryService;
import com.ys.service.amazonorder.TcoDayProductJPEUROPEService;
import com.ys.service.amazonorder.TcoDayProductJPNORTHService;
import com.ys.service.amazonorder.TcoDayProductJPService;
import com.ys.service.amazonorder.TcoDayProductJPSumService;
import com.ys.service.amazonorder.TcoDayRegionService;
import com.ys.service.amazonorder.TcoDayShopService;
import com.ys.service.amazonorder.TcoMonthAllService;
import com.ys.service.amazonorder.TcoMonthCountryService;
import com.ys.service.amazonorder.TcoMonthProductJPEUROPEService;
import com.ys.service.amazonorder.TcoMonthProductJPNORTHService;
import com.ys.service.amazonorder.TcoMonthProductJPService;
import com.ys.service.amazonorder.TcoMonthProductJPSumService;
import com.ys.service.amazonorder.TcoMonthProductService;
import com.ys.service.amazonorder.TcoMonthProductTypeService;
import com.ys.service.amazonorder.TcoMonthProductYPService;
import com.ys.service.amazonorder.TcoMonthProductYPSumService;
import com.ys.service.amazonorder.TcoMonthRegionService;
import com.ys.service.amazonorder.TcoYearAllService;
import com.ys.service.amazonorder.TcoYearCountryService;
import com.ys.service.amazonorder.TcoYearProductJPEUROPEService;
import com.ys.service.amazonorder.TcoYearProductJPNORTHService;
import com.ys.service.amazonorder.TcoYearProductJPService;
import com.ys.service.amazonorder.TcoYearProductJPSumService;
import com.ys.service.amazonorder.TcoYearProductService;
import com.ys.service.amazonorder.TcoYearProductTypeService;
import com.ys.service.amazonorder.TcoYearProductYPService;
import com.ys.service.amazonorder.TcoYearProductYPSumService;
import com.ys.service.amazonorder.TpAmazonSellerReportService;
import com.ys.service.amazonorder.TpAmazonShopOrderDailyService;
import com.ys.util.amazon.AmazonUtil;

@Controller
@RequestMapping("/api/outer/demoXDH")
public class ApiOuterDemoXDHController {

	@Autowired
	private TcoYearAllService tcoYearAllService;
	@Autowired
	private TcoYearCountryService tcoYearCountryService;
	@Autowired
	private TcoYearProductService tcoYearProductService;
	@Autowired
	private TcoYearProductYPService tcoYearProductYPService;
	@Autowired
	private TcoYearProductJPService tcoYearProductJPService;
	@Autowired
	private TcoYearProductJPNORTHService tcoYearProductJPNORTHService;
	@Autowired
	private TcoYearProductJPEUROPEService tcoYearProductJPEUROPEService;
	@Autowired
	private TcoYearProductYPSumService tcoYearProductYPSumService;
	@Autowired
	private TcoYearProductJPSumService tcoYearProductJPSumService;
	@Autowired
	private TcoYearProductTypeService tcoYearProductTypeService;
	@Autowired
	private TcoDayAllService tcoDayAllService;
	@Autowired
	private TcoDayCountryService tcoDayCountryService;
	@Autowired
	private TcoDayRegionService tcoDayRegionService;

	@Autowired
	private TcoDayProductJPService tcoDayProductJPService;
	@Autowired
	private TcoDayProductJPEUROPEService tcoDayProductJPEUROPEService;
	@Autowired
	private TcoDayProductJPNORTHService tcoDayProductJPNORTHService;
	@Autowired
	private TcoDayProductJPSumService tcoDayProductJPSumService;

	@Autowired
	private TcoMonthAllService tcoMonthAllService;
	@Autowired
	private TcoMonthCountryService tcoMonthCountryService;
	@Autowired
	private TcoMonthRegionService tcoMonthRegionService;
	@Autowired
	private TcoMonthProductService tcoMonthProductService;
	@Autowired
	private TcoMonthProductYPService tcoMonthProductYPService;
	@Autowired
	private TcoMonthProductJPService tcoMonthProductJPService;
	@Autowired
	private TcoMonthProductJPEUROPEService tcoMonthProductJPEUROPEService;
	@Autowired
	private TcoMonthProductJPNORTHService tcoMonthProductJPNORTHService;
	@Autowired
	private TcoMonthProductYPSumService tcoMonthProductYPSumService;
	@Autowired
	private TcoMonthProductJPSumService tcoMonthProductJPSumService;
	@Autowired
	private TcoMonthProductTypeService tcoMonthProductTypeService;

	@Autowired
	private TpAmazonSellerReportService tpAmazonSellerReportService;
	
    @Autowired
    private TpAmazonShopOrderDailyService tpAmazonShopOrderDailyService;
    @Autowired
    private TcoDayShopService tcoDayShopService;
	
    @RequestMapping("/test")
	@ResponseBody
	public void test() {
		
		// 连续更新1天内的订单日报表数据
		String dayBegin = "2017-01-01";

		String year = dayBegin.substring(0, 4);
		tcoYearAllService.addBatch(year);
		tcoYearCountryService.addBatch(year);
		tcoYearCountryService.addBatch(year);
		tcoYearProductService.addBatch(year);
		tcoYearProductYPService.addBatch(year);
		tcoYearProductYPSumService.addBatch(year);
		tcoYearProductJPService.addBatch(year);
		tcoYearProductJPEUROPEService.addBatch(year);
		tcoYearProductJPNORTHService.addBatch(year);
		tcoYearProductJPSumService.addBatch(year);
		tcoYearProductTypeService.addBatch(year);
		
		for (int i = 0; i < 12; i++) {
			String month = DateUtil.getYYYY_MM(DateUtil.addMonth(DateUtil.parseYYYY_MM_dd(dayBegin), i));
			tcoMonthAllService.addBatch(month);
			tcoMonthCountryService.addBatch(month);
			tcoMonthRegionService.addBatch(month);
			tcoMonthProductService.addBatch(month);
			tcoMonthProductYPService.addBatch(month);
			tcoMonthProductYPSumService.addBatch(month);
			tcoMonthProductJPService.addBatch(month);
			tcoMonthProductJPNORTHService.addBatch(month);
			tcoMonthProductJPEUROPEService.addBatch(month);
			tcoMonthProductJPSumService.addBatch(month);
			tcoMonthProductTypeService.addBatch(month);
		}
		
			
		for (int i = 0; i < 1000; i++) {
			String day = DateUtil.getYYYY_MM_DD(DateUtil.addDays(dayBegin, i));

			if (day.substring(0, 4).compareTo(dayBegin.substring(0, 4)) > 0) {
				break;
			}
			
			tcoDayAllService.addBatch(day);
			tcoDayCountryService.addBatch(day);
			tcoDayRegionService.addBatch(day);
			tcoDayProductJPService.addBatch(day);
			tcoDayProductJPNORTHService.addBatch(day);
			tcoDayProductJPEUROPEService.addBatch(day);
			tcoDayProductJPSumService.addBatch(day);
			
//			// 添加店铺每日统计
//			tcoDayShopService.addAndUpdateTcoDayShop(day);
		}
		
	}
	
    @RequestMapping("/test2")
	@ResponseBody
	public void test2() {

    	String day = "2024-02-02";
		
		// 添加商品日报表数据
		tpAmazonShopOrderDailyService.addBatch(day);
		
		// 更新商品日报表统计数据
		tpAmazonShopOrderDailyService.updateCount(day);
		
//		// 连续更新1天内的订单日报表数据
//		String dayBegin = "2017-01-01";
//		
//			
//		for (int i = 0; i < 1000; i++) {
//			String day = DateUtil.getYYYY_MM_DD(DateUtil.addDays(dayBegin, i));
//
//			if (day.substring(0, 4).compareTo(dayBegin.substring(0, 4)) > 0) {
//				break;
//			}
//
//			
//			// 添加商品日报表数据
//			tpAmazonShopOrderDailyService.addBatch(day);
//			
//			// 更新商品日报表统计数据
//			tpAmazonShopOrderDailyService.updateCount(day);
//		}
		
	}
	
    @RequestMapping("/test3")
	@ResponseBody
	public void test3() {

		Date now = new Date();
		String latestUpdateDayBegin = DateUtil.getYYYY_MM_DD(DateUtil.addDays(now, -14));
		String latestUpdateDayEnd = DateUtil.getYYYY_MM_DD(now);

		Thread t2 = new Thread(() -> {
			addReport(RegionEnum.NORTH.getCode(), latestUpdateDayBegin, latestUpdateDayEnd);
		});
		t2.start();
		
	}
	
	/**
	 * 添加报告
	 * @param type 类型，1、欧洲，2、北美
	 */
	public void addReport(String regionCode, String latestUpdateDayBegin, String latestUpdateDayEnd) {
		
		Map<String, AWSAuthenticationCredentials> MAP_REGION_Credentials = AmazonUtil.getCredentials();
		AWSAuthenticationCredentialsProvider awsAuthenticationCredentialsProvider = AmazonUtil.getProvider();
		
		// 给每个店铺创建订单报告
		List<TpAmazonSeller> listTpAmazonSeller = new TpAmazonSeller()
				.setAuthState(TpAmazonSellerEnum.AuthState.YES.value)
				.setRegionCode(regionCode)
				.queryList();
		for(TpAmazonSeller entity : listTpAmazonSeller) {
			String sellerId = entity.getSellerId();
			tpAmazonSellerReportService.add(sellerId, latestUpdateDayBegin, latestUpdateDayEnd, MAP_REGION_Credentials, awsAuthenticationCredentialsProvider);
		}
	}
}
